/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.score.web;

import java.io.File;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.score.service.ScoreExchangeService;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 积分对换设置Controller
 * @author Jakemanse
 * @version 2013-09-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/score/scoreExchange")
public class ScoreExchangeController extends BaseController {

	@Autowired
	private ScoreExchangeService scoreExchangeService;
	
	
	@ModelAttribute
	public ScoreExchange get(@RequestParam(required=false) Long id) {
		if (id != null){
			return scoreExchangeService.get(id);
		}else{
			return new ScoreExchange();
		}
	}
	
	@RequiresPermissions("score:scoreExchange:view")
	@RequestMapping(value = {"list", ""})
	public String list(ScoreExchange scoreExchange, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			scoreExchange.setUser(user);
		}
        Page<ScoreExchange> page = scoreExchangeService.find(new Page<ScoreExchange>(request, response), scoreExchange); 
        
        
        model.addAttribute("page", page);
		return "modules/wechat/score/scoreExchangeList";
	}

	@RequiresPermissions("score:scoreExchange:view")
	@RequestMapping(value = "form")
	public String form(ScoreExchange scoreExchange, Model model) {
		scoreExchange.setUser(UserUtils.getUser());
		
		
		model.addAttribute("scoreExchange", scoreExchange);
		return "modules/wechat/score/scoreExchangeForm";
	}

	@RequiresPermissions("score:scoreExchange:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("ShowPicfile") MultipartFile ShowPicfile,HttpServletRequest request, ScoreExchange scoreExchange, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, scoreExchange)){
			return form(scoreExchange, model);
		}
		
		if (AssertUtils.isNull(scoreExchange.getCount())) {
			scoreExchange.setCount(0);
		}
		
		
		if(ShowPicfile != null && ShowPicfile.getOriginalFilename().length()>4){
			String scorePath = WechatUtils.getScoreExchangeProjectResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(scorePath);
			String sname = ShowPicfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_seckill_"+System.currentTimeMillis()+fileType;
			System.out.println(fname);
			File file = new File(scorePath+fname);
			System.out.println("保存文件:"+scorePath+fname);
			try {
				ShowPicfile.transferTo(file);
				scoreExchange.setShowPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+scorePath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		if(scoreExchange.getExchangeNumber()==null ){
			scoreExchange.setExchangeNumber(0);
		}
		if(scoreExchange.getExchangeNumber()<=0){
			scoreExchange.setExchangeNumber(scoreExchange.getNumber());
		}
		
		//修改
//		scoreExchange.setScore((int)(Double.parseDouble(request.getParameter("pScore"))*1000));
		System.out.println(scoreExchange.getUser().getId());
		scoreExchangeService.save(scoreExchange);
		addMessage(redirectAttributes, "保存积分对换设置'" + scoreExchange.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/score/scoreExchange/?repage";
	}
	
	@RequiresPermissions("score:scoreExchange:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		scoreExchangeService.delete(id);
		addMessage(redirectAttributes, "删除积分对换设置成功");
		return "redirect:"+Global.ADMIN_PATH+"/score/scoreExchange/?repage";
	}
	
	
	
	@RequiresPermissions("score:scoreExchange:edit")
	@RequestMapping(value = "update")
	public String updateStatus(Long id,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String status = request.getParameter("status");
		scoreExchangeService.updateStatusById(id, status) ;
		addMessage(redirectAttributes, ("1".equals(status)) ? "核销成功":"作废成功！");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/lotteryRecord/?repage";
	}
	
	

}
